package com.example.firstapplication;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Camera extends Activity implements View.OnClickListener {

	ImageButton ib;
	ImageView iv;
	Button bChange;
	Intent intent;
	final static int cameraResult=0;
	Bitmap bm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picture);
		initialize();
		InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
		bm = BitmapFactory.decodeStream(is);
	}
	
	private void initialize(){
		ib=(ImageButton) findViewById(R.id.ibCamera);
		iv=(ImageView) findViewById(R.id.ivCamera);
		bChange=(Button) findViewById(R.id.bsetWall);
		bChange.setOnClickListener(this);
		ib.setOnClickListener(this);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.bsetWall:
			 try {
				getApplicationContext().setWallpaper(bm);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 break;
		case R.id.ibCamera:
			intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(intent, cameraResult);
			 break;
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			Bundle extras=data.getExtras();
			bm=(Bitmap)extras.get("data");
			iv.setImageBitmap(bm);
		}
	}
}
